/****************************************************************
 * Copyright (c) 2006-2007 Besture All Rights Reserved.
 * 
 * File Name:		BaseDataAccess.cs
 * 
 * Created:		2013/06/24
 * Original Author:	Ni Tao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/
using System;
using System.Web;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Web.UI;
using System.Web.UI.WebControls;

using Besture.Common.Definition;


namespace Besture.Common
{        
    /// <summary>
    /// The base class of business facade layer
    /// </summary>
    public class BaseFacade : CommonBase
    {

        private TransactionManager cdbTransactionManager;

        public BaseFacade()
        {
        }

        /// <summary>
        /// Close connection whith Sql Server 
        /// </summary>
        protected void CloseConnection()
        {
            cdbTransactionManager.CloseConnection();
            RemoveSession();
        }

        /// <summary>
        /// Create connection whith Sql Server 
        /// </summary>
        protected void CreateConnection()
        {
            cdbTransactionManager = new TransactionManager();
            cdbTransactionManager.CreateConnection();
            cdbTransactionManager.OpenConnection();
            cdbTransactionManager.ConnectionStatus = true;
            SaveSession();
        }

        /// <summary>
        /// Begin Transaction for current requirement
        /// </summary>
        protected void BeginTransaction()
        {
            cdbTransactionManager.BeginTransaction();
            cdbTransactionManager.UseTransaction = true;
            SaveSession();
        }

        /// <summary>
        /// Begin Transaction for current requirement by using 
        /// and set specifies of transaction
        /// </summary>
        /// <param name="Level">Specifies of transaction</param>
        protected void BeginTransaction(IsolationLevel Level)
        {
            cdbTransactionManager.BeginTransaction(Level);
            cdbTransactionManager.UseTransaction = true;
            SaveSession();
        }

        /// <summary>
        /// Save TransanctionManager into Session
        /// </summary>
        private void SaveSession()
        {
            HttpContext lhttpContext = HttpContext.Current;
            if (lhttpContext.Session[Constants.SESSION_KEY_TRANSACTION] != null)
            {
                lhttpContext.Session.Remove(Constants.SESSION_KEY_TRANSACTION);
                lhttpContext.Session.Add(Constants.SESSION_KEY_TRANSACTION, cdbTransactionManager);
            }
            else
            {
                lhttpContext.Session.Add(Constants.SESSION_KEY_TRANSACTION, cdbTransactionManager);
            }
        }

        /// <summary>
        /// Remove the transaction from session ,if it be here
        /// </summary>
        private void RemoveSession()
        {
            HttpContext lhttpContext = HttpContext.Current;
            if (lhttpContext.Session[Constants.SESSION_KEY_TRANSACTION] != null)
            {
                lhttpContext.Session.Remove(Constants.SESSION_KEY_TRANSACTION);
            }
        }

        /// <summary>
        /// Commit transaction 
        /// </summary>
        protected  void Commit()
        {
            cdbTransactionManager.Commit();
        }

        /// <summary>
        /// Rollback transaction
        /// </summary>
        protected void RollBack()
        {
            cdbTransactionManager.Rollback();
        }
    }
}
